﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace update
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.label1.Text = "正在下载";
        }
        private void DownLoadCallBack(string data, string code, string result)
        {
             Console.WriteLine(result + "====res==========");
                JObject value = JObject.Parse(result);
                if (value["success"] != null && "true".Equals(value["success"].ToString().ToLower()))
                {
                this.label1.Text = "下载完成，开始更新";
                label1.Refresh();

            }
                else if (value["file"] != null)
                {
                    var file = value["file"].ToString();
                    int end = file.LastIndexOf("/");
                    file = file.Substring(end + 1);
                    var dtotal = value["dtotal"].ToString();
                    var dnow = value["dnow"].ToString();
                    int totalSize = Convert.ToInt32(dtotal);
                    this.progressBar1.Maximum = totalSize;
                   int  currentSize = Convert.ToInt32(dnow);
                    SetTextMessage(currentSize, file);


            }
        }
        private void Button1_Click(object sender, EventArgs e)
        {
            
            var url = "http://172.16.8.67:8090/version.xml";
            //{ "detail":[{"files":[{"file":"test/downtest.txt","size":0},{"file":"test/test.pdf","size":0},{"file":"test/UpdateEx.dll","size":0}],"lversion":"","name":"2.0","sversion":"2.0"}],"message":"","success":true,"update":true}
            //UpdateDll.Check(url, (string data, string code, string result) =>
            //{
            //    Console.WriteLine(result);

            //}, "check");
             UpdateDll.DownLoad(url,  DownLoadCallBack , "download");
            //UpdateDll.Update("-wait 5", (string updateCode, string updateStr, string updateResult) =>
            //{
            //    Console.WriteLine(updateResult);
            //}, "update");
        }
        private delegate void SetPos(int ipos,string text);
        private void SetTextMessage(int ipos,string text)
        {
            if (this.InvokeRequired)
            {
                SetPos setpos = new SetPos(SetTextMessage);
                this.Invoke(setpos, new object[] { ipos, text });
            }
            else
            {
                Console.WriteLine(text + "====text============");
                this.label1.Text = "正在下载:"+ text;
                this.label1.Refresh();
                this.progressBar1.Value = Convert.ToInt32(ipos);
                this.progressBar1.Refresh();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
